# -*- coding:utf-8 -*-
__author__ = 'helloworld'

import cv2

class Camera_reader(object):

    def build_camera(self):
        # opencv文件中人脸级联文件的位置，用于帮助识别图像或者视频流中的人体
        hog = cv2.HOGDescriptor()
        hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())

        # 打开摄像头并开始读取画面
        cameraCapture = cv2.VideoCapture(0)
        success, frame = cameraCapture.read()

        while success and cv2.waitKey(1) == -1:
             success, frame = cameraCapture.read()
             gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)  # 图像灰化
        #     hogs = hog.detectMultiScale(gray, 1.3, 5)   # 识别人脸
             hogs, weights = hog.detectMultiScale(gray,  winStride=(4, 4), padding=(8, 8), scale=1.5)  # 识别行人frame
             num = 0
             for (x, y, w, h) in hogs:
                 show_name = "hog{}".format(num)
                 cv2.putText(frame, show_name, (x, y - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 2)  #显示名字
                 frame = cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)  #在人体区域画一个正方形出来
                 num+=1
             if num>0:
                 print('people-num: %s' % num)  # 打印个数
             cv2.imshow("Camera", frame)

        cameraCapture.release()
        cv2.destroyAllWindows()

if __name__ == '__main__':
    camera = Camera_reader()
    camera.build_camera()